package org.pb.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class Configuration {
	
	private static final Logger logger = Logger.getLogger(Configuration.class);
	
	private static final String IMG_DATA_SOURCE = "PlacardBoys.properties";
	
	private ClassLoader clazzLoader;
	private final int margin;
	private final int fontSize;
	private final int fontBottomMargin;
	private final int boyImgWidth;
	private final int boyImgHeight;
	private final int placardRotateDegree;
	private final int placardShearInnerDegree;
	private final int placardLeftTopCornerX;
	private final int placardLeftBottomCornerY;
	private final int placardCenterX;
	private final int placardCenterY;
	private final int placardHorizontalDistance;
	private final int placardVerticalDistance;
	private final int maxChar;
	private final int rowLength;
	private final String outputName;

	public Configuration(){
		this(IMG_DATA_SOURCE);
	}
	
	public Configuration(String source){
		
		this.clazzLoader = getClass().getClassLoader();
		InputStream is = clazzLoader.getResourceAsStream(source);
		
		Properties props = new Properties();
		try {
			props.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		this.margin = Integer.parseInt(props.getProperty("margin"));
		this.fontSize = Integer.parseInt(props.getProperty("fontSize"));
		this.fontBottomMargin = Integer.parseInt(props.getProperty("fontBottomMargin"));
		this.boyImgWidth = Integer.parseInt(props.getProperty("boyImgWidth"));
		this.boyImgHeight = Integer.parseInt(props.getProperty("boyImgHeight"));
		this.placardRotateDegree = Integer.parseInt(props.getProperty("placardRotateDegree"));
		this.placardShearInnerDegree = Integer.parseInt(props.getProperty("placardShearInnerDegree"));
		this.placardLeftTopCornerX = Integer.parseInt(props.getProperty("placardLeftTopCornerX"));
		this.placardLeftBottomCornerY = Integer.parseInt(props.getProperty("placardLeftBottomCornerY"));
		this.placardCenterX = Integer.parseInt(props.getProperty("placardCenterX"));
		this.placardCenterY = Integer.parseInt(props.getProperty("placardCenterY"));
		this.placardHorizontalDistance = Integer.parseInt(props.getProperty("placardHorizontalDistance"));
		this.placardVerticalDistance = Integer.parseInt(props.getProperty("placardVerticalDistance"));
		this.maxChar = Integer.parseInt(props.getProperty("maxChar"));
		this.rowLength = Integer.parseInt(props.getProperty("rowLength"));
		this.outputName = props.getProperty("outputName");
		
		validating();
		
	}
	
	private void validating(){
		
		if(this.placardShearInnerDegree < 45 || this.placardShearInnerDegree > 89){
			throw new ConfigurationException("placard shear degree must between 45 and 90");
		}
		
	}
	
	public ClassLoader getClazzLoader() {
		return clazzLoader;
	}

	public int getFontSize() {
		return fontSize;
	}

	public int getFontBottomMargin() {
		return fontBottomMargin;
	}

	public int getBoyImgWidth() {
		return boyImgWidth;
	}

	public int getBoyImgHeight() {
		return boyImgHeight;
	}

	public int getPlacardRotateDegree() {
		return placardRotateDegree;
	}
	
	public int getPlacardShearInnerDegree() {
		return placardShearInnerDegree;
	}

	public int getPlacardLeftTopCornerX() {
		return placardLeftTopCornerX;
	}

	public int getPlacardLeftBottomCornerY() {
		return placardLeftBottomCornerY;
	}

	public int getPlacardCenterX() {
		return placardCenterX;
	}

	public int getPlacardCenterY() {
		return placardCenterY;
	}

	public String getOutputName() {
		return outputName;
	}

	public int getPlacardHorizontalDistance() {
		return placardHorizontalDistance;
	}

	public int getPlacardVerticalDistance() {
		return placardVerticalDistance;
	}

	public int getMaxChar() {
		return maxChar;
	}

	public int getRowLength() {
		return rowLength;
	}

	public int getMargin() {
		return margin;
	}
	

}
